package daobeauty.interceptor.invoke.impl;

import java.lang.reflect.Method;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;

import daobeauty.interceptor.invoke.AbstractCurrentSessionInvokeStrategy;
import daobeauty.interceptor.invoke.ArgumentMask;
import daobeauty.interceptor.invoke.parser.Parser;
import daobeauty.interceptor.invoke.parser.operator.Operator;
import daobeauty.interceptor.returntype.executor.criteria.CriteriaExecutor;
import daobeauty.util.ArrayListStack;
import daobeauty.util.EmptyStack;
import daobeauty.util.MethodSignatureUtils;
import daobeauty.util.Stack;

public class FindByParsingInvoke extends AbstractCurrentSessionInvokeStrategy {
	private static final Log LOG = LogFactory.getLog(FindByParsingInvoke.class);
	private final CriteriaExecutor executor;
	private final ArgumentMask argumentMask;
// 	private final Set<String> domainProperties;
	private final Operator operator; 
	private final ArgumentStackStrategy stackStrategy;
	
	@SuppressWarnings("unchecked")
	public FindByParsingInvoke(Method method, SessionFactory sessionFactory, Class domainClass, Set<String> domainProperties) {
		super(sessionFactory, domainClass);
		executor = new CriteriaExecutor(method.getReturnType());
		argumentMask = new ArgumentMask(method, 0);
//		this.domainProperties = domainProperties;
		final String methodNameWithoutPrefix = method.getName().substring(6); // remove "findBy"
		operator = new Parser(methodNameWithoutPrefix, domainProperties, argumentMask.getFilteredParameterTypes()).parse();
		if (argumentMask.getFilteredArgumentsSize()<=0){
			stackStrategy = new EmptyStackStrategy();
		}
		else{
			stackStrategy = new ArrayListStackStrategy();
		}
		if (LOG.isInfoEnabled()){
			LOG.info("created findBy strategy with parsing for method "+MethodSignatureUtils.toMethodSignature(method));
			LOG.info("Query with "+operator.getTotalAmountArguments()+" parameters is: "+operator.toTotalString());
		}
		if (operator.getTotalAmountArguments()!=argumentMask.getFilteredArgumentsSize()){
			LOG.warn("Amount of needed parameters for query ("+operator.getTotalAmountArguments()+") and defined parameters in method ("+argumentMask.getFilteredArgumentsSize()+") don't match.");	
		}
	}
	
	@SuppressWarnings("unchecked")
	public Object invoke(String methodName, Object[] args) throws Exception {
		final Criteria criteria = createCriteria();
		criteria.add(operator.createCriterion(stackStrategy.getArgumentStack(argumentMask.getFiltered(args))));
		return executor.execute(criteria, argumentMask.getLimiting(args), argumentMask.getSorting(args));
	}
	
	private interface ArgumentStackStrategy{
		public Stack getArgumentStack(Object [] arguments);
	}
	
	private static class EmptyStackStrategy implements ArgumentStackStrategy{
		public Stack getArgumentStack(Object[] arguments) {
			return EmptyStack.INSTANCE;
		}
	}

	private static class ArrayListStackStrategy implements ArgumentStackStrategy{
		@SuppressWarnings("unchecked")
		public Stack getArgumentStack(Object[] arguments) {
			final ArrayListStack stack = new ArrayListStack(arguments.length);
			for (int i=arguments.length-1; i>=0; i--){
				stack.push(arguments[i]);
			}
			return stack;
		}
	}

}
